package com.powernobug.servlet;

import com.powernobug.bean.model.MarketAdmin;
import com.powernobug.bean.vo.BaseRespVo;
import com.powernobug.bean.vo.PageVo;
import com.powernobug.bean.vo.param.AdminListParam;
import com.powernobug.service.AdminAdminService;
import com.powernobug.service.impl.AdminAdminServiceImpl;
import com.powernobug.util.JsonUtil;
import com.powernobug.util.QueryParamPackageUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Author: 肖云戈
 * @CreateTime: 2024-08-23
 * @Description:
 */
@WebServlet("/admin/*")
public class AdminAdminServlet extends CommonServlet{
    private static AdminAdminService adminAdminService = new AdminAdminServiceImpl();

    public BaseRespVo list(HttpServletRequest req) {
        AdminListParam adminListParam = QueryParamPackageUtil.packParams(AdminListParam.class, req);
        PageVo pageVo = adminAdminService.getAdminByPage(adminListParam);
        return BaseRespVo.okList(pageVo);
    }

    public BaseRespVo create(HttpServletRequest req) throws IOException {
        MarketAdmin marketAdmin = JsonUtil.readJson(req.getReader().readLine(), MarketAdmin.class);
        marketAdmin = adminAdminService.create(marketAdmin);
        return BaseRespVo.okList(marketAdmin);
    }

    public BaseRespVo update(HttpServletRequest req) throws IOException {
        MarketAdmin marketAdmin = JsonUtil.readJson(req.getReader().readLine(), MarketAdmin.class);
        Boolean isSuccess = adminAdminService.update(marketAdmin, false);
        return isSuccess ? BaseRespVo.okList(marketAdmin) : BaseRespVo.errList(801);
    }

    public BaseRespVo delete(HttpServletRequest req) throws IOException {
        MarketAdmin marketAdmin = JsonUtil.readJson(req.getReader().readLine(), MarketAdmin.class);
        Boolean isSuccess = adminAdminService.update(marketAdmin, true);
        return isSuccess ? BaseRespVo.okList(null) : BaseRespVo.errList(802);
    }
}
